package database

import (
	"admin/model"
	"log"

	_ "github.com/mattn/go-sqlite3"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

var db *gorm.DB

// 获取db连接
func Db() *gorm.DB {
	if db != nil {
		return db
	}
	sdb, err := gorm.Open(sqlite.Open("database.db"), &gorm.Config{})
	if err != nil {
		log.Fatalln("数据库连接异常，已终止服务器：", err)
		return nil
	}
	db = sdb
	return sdb
}

func InitDataBase() {
	db := Db()
	// 初始化数据库
	db.AutoMigrate(&model.User{})
	db.AutoMigrate(&model.Student{})
	db.AutoMigrate(&model.Classes{})
	db.AutoMigrate(&model.Department{})
}
